package org.ras.service;

import org.ras.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Title: Account服务类
 * CreateDate:  2018/1/31
 *
 * @author houdengw
 * @version 1.0
 */
@Service
public class AccountService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 获取所有账号
     * @return
     */
    public List<Account> getAccounts(){

        String sql = "select userid,nickname,username,name,sex,email,mobile,status,insert_time from ras_sso_account";
        return (List<Account>)jdbcTemplate.query(sql, new AccountRowMapper());

    }

    /**
     * 保存账号
     * @param account
     * @return
     */
    public Account saveAccount(Account account){

        String sql = "insert into ras_sso_account(userid,username,password,sex,name,nickName,email,mobile,status,insert_time) values(?,?,?,?,?,?,?,?,?,?)";
        int resRow = jdbcTemplate.update(sql,new Object[]{UUID.randomUUID().toString().replaceAll("-", ""),account.getUsername(),"123456",account.getSex(),account.getName(),account.getNickname(),account.getEmail(),account.getMobile(),"1",new Date()});
        if(resRow > 0)
            return getAccountByUsername(account.getUsername());
        else
            return null;

    }

    /**
     * 根据用户名获取账号
     * @param username
     * @return
     */
    public Account getAccountByUsername(String username){
        Account account = null;
        String sql = "select userid,nickname,username,name,sex,email,mobile,status,insert_time from ras_sso_account where username =?";
        List<Account> accountlist = (List<Account>)jdbcTemplate.query(sql,new Object[]{username},new AccountRowMapper());
        if(accountlist.size()>0 && accountlist!=null){
            account = accountlist.get(0);
        }
        return account;
    }

    /**
     * 根据userid获取用户
     * @param userid
     * @return
     */
    public Account getAccountByUserId(String userid){
        Account account = null;
        String sql = "select userid,nickname,username,name,sex,email,mobile,status,insert_time from ras_sso_account where userid =?";
        List<Account> accountlist = (List<Account>)jdbcTemplate.query(sql,new Object[]{userid},new AccountRowMapper());
        if(accountlist.size()>0 && accountlist!=null){
            account = accountlist.get(0);
        }
        return account;
    }



}

class AccountRowMapper implements RowMapper<Account>{

    @Override
    public Account mapRow(ResultSet resultSet, int i) throws SQLException {
        Account account = new Account();
        account.setUserid(resultSet.getString("userid"));
        account.setNickname(resultSet.getString("nickname"));
        account.setUsername(resultSet.getString("username"));
        account.setName(resultSet.getString("name"));
        account.setSex(resultSet.getString("sex"));
        account.setEmail(resultSet.getString("email"));
        account.setMobile(resultSet.getString("mobile"));
        account.setStatus(resultSet.getString("status"));
        account.setInsert_time(resultSet.getDate("insert_time"));
        return account;
    }

}
